<?

	class Fmgr0v0r01_draft extends Fmgr0v0r01_dir{
		
		private $index;
		
		private function copy_original(){
			if((!$file = $this->original())||(!$new = $file->copy_to($this))){
				$this->destroy();
				return false;
			}
			return $this->index_new($new);
		}
		
		function original(){
			return $this->db()->get($this->basename());	
		}
		
		function redraft($index=false){
			if(!$this->meta()->length() || ($index == $this->basename())) return $this->copy_original();
			return $this->index_new($this->get_draft($index)->duplicate());
		}
		
		function rewrite($dfil){
			if(!$this->isMyChild($dfil)) return false;
			$if = $dfil->realpath();
			$of = $this->original()->realpath();
			(unlink($of) && copy($if, $of));
			$this->original()->check('checksum');
			$this->original()->commit();
		}
		
		function get_draft($index=false){
			if(!$index || ($index == $this->basename())) $index = $this->meta()->lastkey();
			return $this->db()->get($index);
		}

		function latest(){
			if(!$this->meta()->length()) return $this->original();
			return $this->db()->get($this->meta()->lastkey());
		}
		
		private function index_new($new){
			$this->meta(1)->set($new->id, $new->created);
			$this->commit();
			return $new;
		}
		
		function discard($dfil){
			if(!$dfil || !$this->isMyChild($dfil)) return false;
			if(!unlink($dfil->realpath()) || !$dfil->destroy()) return false;
			$this->meta()->remove($dfil->id);
			$this->commit();
			return true;
		}
		
		function is_destroyable(){
			return true;
		}
		
		function destroy(){
			foreach($this->contents() as $f) unlink($this->realpath($f));
			if(is_dir($this->realpath())) rmdir($this->realpath());
			return parent::destroy();
		}
	
	}

?>